package views.homme;

import java.awt.event.ActionEvent;

import javax.swing.AbstractAction;
import javax.swing.JTable;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;

public class ShowColumnAction extends AbstractAction 
{
	private int[] columnsToShow;
	private TableColumnModel tcm;
	private TableColumn[] columns;
	private JTable table;
	private String label;

	public ShowColumnAction(String label, int columnsToShow[], JTable table)
	{
		super(label);
		this.label = label;
		this.columnsToShow = columnsToShow;
		this.table = table;
		this.tcm = this.table.getColumnModel();

		this.columns = new TableColumn[table.getColumnCount()];

		for (int i = 0; i < columns.length; i++)
			this.columns[i] = table.getColumnModel().getColumn(i);
	}

	public void actionPerformed(ActionEvent e) 
	{
		TableColumn tc;

		int colIndex;
		// Loop to remove all columns from table
		for (colIndex = 0; colIndex < columns.length; colIndex++)
			tcm.removeColumn(columns[colIndex]);
		// Loop on all columns
		for (colIndex = 0; colIndex < columns.length; colIndex++)
			// For each column to show which index is stored in the
			// columnsToShow array
			for (int i = 0; i < columnsToShow.length; i++)
				// If column index is the same as the index from the array
				// ...
				if (columnsToShow[i] == colIndex)
				{
					// ... get the column
					tc = columns[colIndex];
					// ... and add it to the table
					tcm.addColumn(tc);
				}
	}
}
